package com.ssyt.hiring.talent.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.common.App;
import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.talent.entity.ResumeEntity;
import com.ssyt.hiring.talent.entity.TalentEntity;
import com.ssyt.hiring.talent.service.ResumeService;
import com.ssyt.hiring.talent.service.TalentService;

public class TalentServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private TalentService talentService = new TalentService();
    private ResumeService resumeService = new ResumeService();

    public TalentServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding(Constants.ENCODE_UTF8);
        String command = request.getParameter("command");
        if ("toLoginUI".equals(command)) {
            toLoginUI(request, response);
        } else if ("login".equals(command)) {
            login(request, response);
        } else if ("logout".equals(command)) {
            logout(request, response);
        } else if ("main".equals(command)) {
            main(request, response);
        }
    }

    /**
     * 到求职者登录页面
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void toLoginUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/view/talent/login.jsp").forward(request, response);
    }

    /**
     * 求职者登录
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code = request.getParameter("code");
        String password = request.getParameter("password");

        TalentEntity talent = talentService.login(code, password);

        if (talent == null) {
            request.setAttribute("msg", "用户名或密码错误！");
            request.getRequestDispatcher("/WEB-INF/view/talent/login.jsp").forward(request, response);
        } else {
            request.getSession().setAttribute("talent", talent);
            main(request, response);
        }
    }

    /**
     * 系统注销
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    /**
     * 求职者后台
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int talentId = App.getTalentId(request, response);
        List<ResumeEntity> resumeList = resumeService.listResumeByTalentId(talentId);
        request.setAttribute("resumeList", resumeList);
        request.getRequestDispatcher("/WEB-INF/view/talent/main.jsp").forward(request, response);
    }

}
